package com.zerui.manager.device.mapper;

import com.zerui.manager.common.mapper.EasyBaseMapper;
import com.zerui.manager.device.custom.DeviceCustom;
import com.zerui.manager.device.entity.DeviceDo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.sql.Time;
import java.util.List;
import java.util.Set;

/**
 * @Author wh
 * @create_time 2023-08-23 15:28
 */
@Mapper
public interface DeviceMapper extends EasyBaseMapper<DeviceDo> {

    @Select({"<script>",
            "SELECT td.id,td.device_id,td.access,td.enable_status,td.curfew_status,td.timed_task_status,td.start_time,td.end_time,td.create_time"
                    + ",tdt.device_from"
                    + " FROM tb_device td"
                    + " LEFT JOIN tb_device_touch tdt on tdt.device_id = td.device_id"
                    + " WHERE tdt.del_flag = 0",
            "<if test=\"deviceIds != null and deviceIds.size>0\">",
            "   and td.device_id in <foreach collection='deviceIds' item='id' open='(' separator=',' close=')'>",
            "${id}</foreach>",
            "</if>",
            "</script>"})
    List<DeviceCustom> selectByDeviceIds2(@Param("deviceIds") Set<Long> deviceIds);

    @Select({"<script>",
            "SELECT td.id,td.device_id,td.access,td.enable_status,td.curfew_status,td.timed_task_status,td.start_time,td.end_time,td.create_time"
                    + ",tdt.device_from"
                    + " FROM tb_device td"
                    + " LEFT JOIN tb_device_touch tdt on tdt.device_id = td.device_id"
                    + " WHERE tdt.del_flag = 0",
            "<if test=\"accessStatus!=null\">",
            "   and td.timed_task_status = #{accessStatus}",
            "</if>",
            "<if test=\"startTime != null\">",
            "   and td.start_time >= #{startTime}",
            "</if>",
            "<if test=\"endTime != null\">",
            "   and td.start_time &lt;= #{endTime}",
            "</if>",
            "</script>"})
    List<DeviceCustom> selectDeviceCurfewTimeStart(@Param("startTime") Time startTime, @Param("endTime") Time endTime, @Param("accessStatus") Integer accessStatus);

    @Select({"<script>",
            "SELECT td.id,td.device_id,td.access,td.enable_status,td.curfew_status,td.timed_task_status,td.start_time,td.end_time,td.create_time"
                    + ",tdt.device_from"
                    + " FROM tb_device td"
                    + " LEFT JOIN tb_device_touch tdt on tdt.device_id = td.device_id"
                    + " WHERE tdt.del_flag = 0",
            "<if test=\"accessStatus!=null\">",
            "   and td.timed_task_status = #{accessStatus}",
            "</if>",
            "<if test=\"startTime != null\">",
            "   and td.end_time >= #{startTime}",
            "</if>",
            "<if test=\"endTime != null\">",
            "   and td.end_time &lt;= #{endTime}",
            "</if>",
            "</script>"})
    List<DeviceCustom> selectDeviceCurfewTimeEnd(@Param("startTime") Time startTime, @Param("endTime") Time endTime, @Param("accessStatus") Integer accessStatus);
}
